/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hiof.moraa.server.service;

import com.hiof.moraa.server.dao.AthleteLiveLocationDAO;
import com.hiof.moraa.server.service.model.Feed;
import com.hiof.moraa.server.service.model.GeoPoint;
import com.hiof.moraa.server.service.model.LiveLocationFeed;
import com.hiof.moraa.server.service.model.TypeOfAttendant;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;

/**
 * @author Bergy
 */
@Transactional(readOnly = false)
public class AthleteLiveLocationServiceImp implements LiveLocationService {

    AthleteLiveLocationDAO dao;
    CheckPassTargetService checkPassTargetService;
    MessageDeliveryService messageDeliveryService;

    public void setCheckPassTargetService(CheckPassTargetService checkPassTargetService) {
        this.checkPassTargetService = checkPassTargetService;
    }

    public void setDao(AthleteLiveLocationDAO dao) {
        this.dao = dao;
    }

    public void setMessageDeliveryService(MessageDeliveryService messageDeliveryService) {
        this.messageDeliveryService = messageDeliveryService;
    }

    @Override
    public Feed updateLiveLocationFeed(int userId, int athleteId, double lat, double lng, double height, double speed) {
        LiveLocationFeed liveLocationFeed = dao.insertAthleteLiveLocationFeed(athleteId, lat, lng, height, speed, new Timestamp(new java.util.Date().getTime()));
        messageDeliveryService.deliveryLiveLocationMessage(userId, TypeOfAttendant.ATHLETE, athleteId, liveLocationFeed);
        checkPassTargetService.checkPassTarget(userId, athleteId, lat, lng, height);
        return liveLocationFeed;
    }

    @Override
    public GeoPoint getLatestLocation(int athleteId) {
        LiveLocationFeed liveLocationFeed = dao.getLatestLiveLocationFeedOfOneAthlete(athleteId);
        if (liveLocationFeed != null) {
            return liveLocationFeed.getGoogleGeoPoint();
        } else {
            return null;
        }
    }
}
